package com.zret.oasys.operation.leave;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.entity.Attence;
import com.zret.oasys.json.entity.MsgJson;

public class LeaveApproval implements LeaveServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
		// 编码设置
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		// ajax相关的传输对象与格式转换对象
		MsgJson msg = new MsgJson();
		Gson gson = new Gson();
		// 输出六
		PrintWriter out = response.getWriter();
		// 获取请假流水号, 与审批单号, 审批评语
		String leavenum = "";
		int detailid = 0;
		String comment = "";
		int empid = 0;
		String leavetype = "";
		if(request.getParameter("leavenum") != null) {
			leavenum = request.getParameter("leavenum");
		}
		if(request.getParameter("detailid") != null) {
			detailid = Integer.valueOf(request.getParameter("detailid"));
		}
		if(request.getParameter("comment") != null) {
			comment = request.getParameter("comment");
		}
		if(request.getParameter("empid") != null) {
			empid = Integer.valueOf(request.getParameter("empid"));
		}
		if(request.getParameter("leavetype") != null) {
			leavetype = request.getParameter("leavetype");
		}
		// 执行审批完成操作
		boolean flag =  leaveService.leaveApproval(leavenum, detailid, comment);
		// 向打卡记录中插入一条请假操作
		String date = simple.format(new Date());
		date += " 05:00:00"; 
		Attence attence = new Attence(empid, date, leavetype);
		System.out.println(attence);
		attendanceService.addAttendance(attence);
		// 写入执行结果的信息 
		if(flag) {
			msg.setCode(200);
			msg.setMsg("审批完成!");
		} else {
			msg.setCode(200);
			msg.setMsg("审批完成!");
		}
		String jsonObj = gson.toJson(msg);
		System.out.println(jsonObj);
		// ajax请求返回
		out.print(jsonObj);
		out.close();
	}

}
